package com.heima.common.aliyun.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.heima.common.aliyun.properties.GreenProperties;
import com.heima.common.aliyun.service.AliyunGreenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author: itheima
 * @create: 2021-05-31 00:48
 */
@Slf4j
@Configuration
@Import({AliyunGreenService.class})
@EnableConfigurationProperties(value = {GreenProperties.class}) //启动配置文件
@ConditionalOnProperty(prefix = "aliyun.green", value = "enable", havingValue = "true")
public class GreenConfig {

    @Autowired
    private GreenProperties greenProperties;

    @Bean
    public IAcsClient iAcsClient(){
        try {
            IClientProfile profile = DefaultProfile
                    .getProfile("cn-shanghai", greenProperties.getAccessKeyID(), greenProperties.getAccessKeySecret());
            IAcsClient client = new DefaultAcsClient(profile);
            return client;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("Green配置缺失，请补充!");
            return null;
        }
    }
}
